package cn.itsource.crm.web.controller;

import cn.itsource.crm.domain.Tenant;
import cn.itsource.crm.query.TenantQuery;
import cn.itsource.crm.service.ITenantService;
import cn.itsource.util.AjaxResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/tenant")
@CrossOrigin
public class TenantController {

    @Autowired
    private ITenantService tenantService;

    /**
     * 查询所有数据
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)//批量查询用PATCH
    @ResponseBody
    @ApiOperation(value = "list查询所有方法",notes = "查询所有租户信息")
    //HTTP响应整体描述
    @ApiResponses({
            //HTTP响应其中1个描述
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 500,message = "网络错误，请稍后")
    })
    public List<Tenant> list(){
        return tenantService.findAll();
    }

    /**
     * 根据公司名查询公司是否存在
     * @param
     * @return
     */
    @RequestMapping(value = "/queryByName",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据公司名查询公司是否存在",notes = "根据公司名查询公司是否存在")
    //一个请求参数
    @ApiImplicitParam(name = "companyName",value = "公司名参数",required = true,dataType = "String",paramType = "path")
    public Tenant queryByName(@RequestBody TenantQuery query){
        Tenant tenant = tenantService.queryByName(query);
        return tenant;
    }

    /**
     * 根据id查询单个租户信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/findOne/{id}",method = RequestMethod.GET)//查询单个用GET
    @ResponseBody
    //描述一个类的一个方法，或者说一个接口
    @ApiOperation(value = "根据id查询租户信息",notes = "getOne查询方法")
    //一个请求参数
    @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
    public Tenant findOne(@PathVariable Long id){
        return tenantService.findOne(id);
    }

    /**
     * 保存租户信息
     * @param tenant
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.PUT)//保存用PUT
    @ResponseBody
    @ApiOperation(value = "save保存租户信息的方法",notes = "保存租户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenant",value = "tenant对象",dataType = "Tenant",paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "保存成功"),
            @ApiResponse(code = 500,message = "服务器错误")
    })
    public AjaxResult save(@RequestBody Tenant tenant){
        try {
            tenantService.save(tenant);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }

    /**
     * 更新租户信息
     * @param tenant
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)//POST用来修改信息
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "更新成功"),
            @ApiResponse(code = 500,message = "网络错误...")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenant租户信息",value = "租户信息",dataType = "Tenant",paramType = "path")
    })
    public AjaxResult update(@RequestBody Tenant tenant){
        try {
            tenantService.update(tenant);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }


    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)//DELETE删除租户信息
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除成功"),
            @ApiResponse(code = 500,message = "服务器异常")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "参数id",value = "参数id",dataType = "Long",paramType = "path",required = true)
    })
    @ApiOperation(value = "delete删除方法",notes = "删除租户信息")
    public AjaxResult delete(@PathVariable Long id){
        try {
            tenantService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }
}
